package com.example.myanimator.mcanvas

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 * @author tgw
 * @date 2021/4/12
 * @describe 自定义圆形图像被选中标记
 */
class SelectCircleImageView : View {
    private var mPaint: Paint? = null
    private var mPathMeasure: PathMeasure? = null
    private var mPath: Path? = null
    private val mContentBounds = Rect() //承载文本宽度

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
        init()
    }

    private fun init() {
        mPaint = Paint()
        mPaint!!.isAntiAlias = true // 去除锯齿
        mPaint!!.strokeWidth = 5f
        mPaint!!.style = Paint.Style.FILL
        mPaint!!.color = Color.YELLOW
        mPaint!!.textAlign = Paint.Align.CENTER
        mPath = Path()
        mPath!!.addCircle(100f, 100f, 50f, Path.Direction.CW)
        mPathMeasure = PathMeasure(mPath, false)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawPath(mPath!!, mPaint!!)
        mPaint!!.color = Color.BLACK
        mPath = Path()
        mPathMeasure!!.getSegment(mPathMeasure!!.length * 0.05f, mPathMeasure!!.length * 0.45f, mPath, true)
        canvas.drawPath(mPath!!, mPaint!!)
        mPaint!!.color = Color.BLUE
        mPaint!!.getTextBounds("已选中", 0,
                3, mContentBounds)
        canvas.drawText("已选中", 100f, (100 + mContentBounds.height()).toFloat(), mPaint!!)
        //        canvas.drawText("已选中",100,mPaint.getFontMetrics(),mPaint);
    }
}